Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    value: "",
    list: [],
    level: "",
    pageNum: 1,
    pageSize: 20,
    options: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options);
    this.setData({
      options: options
    })
    this.initInfos()
  },
   // 滑动底部加载更多
   scrollBottom () {
    this.setData({
      pageNum: ++this.data.pageNum,
    });
    this.initInfos();
  },
  initInfos () {
    if (this.isAdminFilter()) {
      this.setData({
        id: this.data.options.id,
        level: '1'
      })
      this.queryDescendantNodes()
      // if (options.grade == "一级行") {
      //   this.getListById()
      // } else {
      //   this.queryallrootbank()
      // }
    } else if (this.isSecondFilterfunction()) {
      this.setData({
        id: this.data.options.id,
        level: '2'
      })
      if (this.data.options.grade == "一级行") {
        this.getListById()
      } else {
        this.queryallrootbank()
      }
    } else {
      this.setData({
        id: this.data.options.id,
        level: '3'
      })
      this.queryDescendantNodes()
    }
  },
  queryDescendantNodes () {
    let id = wx.getStorageSync("userInfo").agencyCode
    // 41d1f7cf6b874178a72d034caa454649"
    let params = {
      organizationId: id,
      name: this.data.serachValue,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    console.log(params);
    wx.showLoading({
      title: "加载中",
    });
    getApp().globalData.request(
      "/corpVisit/queryOrganizationPage", params,
      "POST"
    ).then(res => {
      wx.hideLoading();
      console.log(res);
      this.setData({
        list: this.data.list.concat(res.data.list)
      })
    })
  },
  queryallrootbank () {
    getApp().globalData.request(
      "/corpVisit/query_all_root_bank", {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    },
      "POST"
    ).then(res => {
      console.log(res)
      if (res.code == 200) {
        this.setData({
          list: this.data.list.concat(res.data.list)
        })
      }
    })

  },
  // 根据id查询二级行
  getListById () {
    getApp().globalData.request(
      "/corpVisit/getOrganizationList", {
      name: this.data.value,
      organizationId: this.data.id
    },
      "POST"
    ).then(res => {
      wx.hideLoading();
      // console.log(res);
      this.setData({
        list: res.data
      })
    })
  },
  // 是否是一级管理
  isAdminFilter () {
    let roleArr = wx.getStorageSync('userInfo').corpRole
    var rusult = roleArr.some(function (item) {
      return item == "fir_admin"
    })
    return rusult
  },
  // 是否是银监
  isSecondFilterfunction () {
    let roleArr = wx.getStorageSync('userInfo').corpRole
    var rusult = roleArr.some(function (item) {
      return item == "cbrc"
    })
    return rusult
  },

  toNextClick (e) {
    console.log(e.currentTarget.dataset.orizid);
    if (this.isAdminFilter()) {
      if (e.currentTarget.dataset.name == "二级行") {
        wx.navigateTo({
          url: `/sub_allotPro/module2/page1_1/page1_1?id=${e.currentTarget.dataset.orizid}&index=${e.currentTarget.dataset.index}&bankname=${e.currentTarget.dataset.bankname}`,
        })
      } else {
        wx.navigateTo({
          url: `/sub_allotPro/module2/page2/page2?id=${e.currentTarget.dataset.orizid}&index=${e.currentTarget.dataset.index}&bankname=${e.currentTarget.dataset.bankname}`,
        })
      }

    } else {
      if (e.currentTarget.dataset.name == "一级行") {
        wx.navigateTo({
          url: `/sub_allotPro/module2/page1/page1?id=${e.currentTarget.dataset.orizid}&grade=${e.currentTarget.dataset.name}`,
        })
      } else {
        wx.navigateTo({
          url: `/sub_allotPro/module2/page1_1/page1_1?id=${e.currentTarget.dataset.orizid}&index=${e.currentTarget.dataset.index}&bankname=${e.currentTarget.dataset.bankname}`,
        })
      }
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})